package com.zjut.azhen.DBDesign.controllers.scores;

import com.zjut.azhen.DBDesign.bean.Report;
import com.zjut.azhen.DBDesign.bean.RespBean;
import com.zjut.azhen.DBDesign.bean.Role;
import com.zjut.azhen.DBDesign.bean.User;
import com.zjut.azhen.DBDesign.service.ScoreService;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/*
 * TODO: 1)学生成绩按每学年进行成绩统计；
         2)学生成绩名次排定；
         4)学生成绩查询；
         5)输入每个学生成绩时，自动生成该学生已修总学分；
 * */

/**
 * @author asus
 */
@RestController

public class ScoreController {
    private final ScoreService scoreService;
    public ScoreController(ScoreService scoreService){
        this.scoreService=scoreService;
    }

    /**
     * @return 学生成绩按每学年进行成绩统计；
     */
        @GetMapping("/score/year")
        public RespBean getScoreYearsBySno(HttpSession session){
            User user=(User)session.getAttribute("user");
            String sno=user.getId();
            System.out.println(sno);
            return RespBean.ok("success",scoreService.getScoreYearsBySno(sno)); }

        @GetMapping("/score/rank/{term}/{mno}")
        public RespBean getRankedScore(@PathVariable String term,@PathVariable String mno){
        if(mno==null){
            return RespBean.error("Parameter Error",mno);
        }
            return RespBean.ok("success",scoreService.getRankedScore(term,mno));
        }

        @GetMapping("/score/avg")
        public RespBean getAvgScoreBySno(HttpSession session){

            Role role=(Role) session.getAttribute("role");
            User user=(User) session.getAttribute("user");

            //学生访问
            if(user==null||!"R02".equals(role.getRno())){
                return RespBean.error("无效访问");
            }
            String sno=user.getId();

            return RespBean.ok("success",scoreService.getAvgScoreBySno(sno));
        }

        @GetMapping("/score/credit")
        public RespBean getCredit(HttpSession session){

            User user=(User)session.getAttribute("user");
            if(user==null){
                return RespBean.error("登录失效");
            }
            return RespBean.ok("success",scoreService.getCredit(user.getId()));
        }
        @GetMapping("/score/score")
        public RespBean getScore(HttpSession session){
            User user =(User)session.getAttribute("user");
            if(user==null){
                return RespBean.error("登录失效");
            }
            return RespBean.ok("success",scoreService.getScore(user.getId()));
        }



}


